using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using LS.Digitize;

namespace LS.GameKit.Digitize
{
    [System.Serializable]
    public class AssetReferencesConfig
    {
        public List<AssetInfo>  assets = new List<AssetInfo>();
        
        [SerializeField]
        private int m_Version;

        public int Version => m_Version;

        public bool ContainsGuid(string guid)
        {
            for (int i = 0; i < assets.Count; i++)
            {
                if (assets[i].Guid == guid)
                    return true;
            }

            return false;
        }

        public bool ContainsFullPath(string path)
        {
            for (int i = 0; i < assets.Count; i++)
            {
                if (assets[i].FullPath == path)
                    return true;
            }

            return false;
        }
        
        public AssetInfo GetAsset(int index)
        {
            return assets[index];
        }
        
        public AssetInfo GetAssetByName(string name)
        {
            return assets.FirstOrDefault(a=>a.FileName == name);
        }
        
        public AssetInfo GetAssetByGuid(string guid)
        {
            return assets.FirstOrDefault(a=>a.Guid == guid);
        }
        
        public AssetInfo GetAssetByFullPath(string fullPath)
        {
            return assets.FirstOrDefault(a=>a.FullPath == fullPath);
        }
        
        public bool RegisterAsset(AssetInfo asset)
        {
            if (!assets.Contains(asset))
            {
                assets.Add(asset);
                return true;
            }
            return false;
        }

        public bool RegisterAsset(string fullPath, string guid = null)
        {
            
            if (!ContainsFullPath(fullPath))
            {
                assets.Add(new AssetInfo(fullPath, guid));
                return true;
            }
            return false;
        }
        
        public bool RemoveAsset(string fullPath, string guid = null)
        {
            int index = assets.FindIndex(a => a.FileName == fullPath);
            if (index >= 0)
            {
                assets.RemoveAt(index);
                return true;
            }
            return false;
        }

        public AssetInfo[] GetAllAsset()
        {
            return assets.ToArray();
        }
    }
}
